package com.zjm.session.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author ZJM
 * @date 2024/4/25 21:53
 */
@WebServlet("/sessionServlet")
public class SessionServlet extends BaseServlet {

    /**
     * 创建和获取session会话对象
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void createOrGetSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建和获取session会话对象
        HttpSession session = req.getSession();
        //判断当前session会话，是否是新创建出来的
        boolean isNewSession = session.isNew();
        //获取session会话的唯一标识id
        String id = session.getId();

        resp.getWriter().write("创建和获取成功<br/>");
        resp.getWriter().write("SessionID" + id + "<br/>");
        resp.getWriter().write("是否新创建的" + isNewSession + "<br/>");
    }

    /**
     * session中保存数据
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void setAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        session.setAttribute("key1", "zjmshdjgahj"); //保存数据
        resp.getWriter().write("保存成功");
    }

    /**
     * session中获取数据
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Object key1 = session.getAttribute("key1"); //获取数据
        resp.getWriter().write("session中获取到的值：" + key1);
    }

    /**
     * 获取session默认超时时长
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        int maxInactiveInterval = session.getMaxInactiveInterval(); //获取超时时长
        resp.getWriter().write("session默认过期时长:" + maxInactiveInterval+"秒");
    }

    /**
     * 设置3秒超时
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void life3(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        session.setMaxInactiveInterval(3); //3秒超时
        resp.getWriter().write("设置session，3秒失效");
    }

    /**
     * 使session失效
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        session.invalidate(); //session马上超时
        resp.getWriter().write("session立即失效");

    }

}
